package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrSpecCache;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrSpec;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description 功能产品 延期属性组属性设置处理类
 * @Author deng.gang
 * @Date 2021/12/22 4:47 下午
 */
public class DelayGroupAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    private static final String MODEL_FIELD_NAME = "200002772";

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        if (this.isHandle(data)) {
            StringBuffer attrSpecNbr = new StringBuffer(inst.getSpecId()).append(SymbolConsts.UNDERLINE).append(attr.getFieldName());
            AttrSpec attrSpec = AttrSpecCache.get(attrSpecNbr.toString());
            KiteProdInst accessProdInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(), inst.getInstId());
            if (Objects.nonNull(attrSpec) && Objects.nonNull(accessProdInst)) {
                String offerId = Optional.ofNullable(querySceneInstService.getMainOfferInstByProdInstId(data.getCustId(), accessProdInst.getProdInstId())).map(KiteOfferInst::getOfferId).orElse("");
                // 销售价结算价存在联动情况
                String specialVal = this.specialHandle(accessProdInst, offerId);
                List<AttrValue> attrValueList = AttrValueCache.get(attrSpec.getAttrId()).stream().filter(s ->
                    KiteStringUtils.equalsAny(s.getAttrValueCode(), accessProdInst.getProdId(), offerId, specialVal)).collect(Collectors.toList());
                this.dealAttrValue(attr, inst, data, attrValueList);
            }
        }
        super.after(attr, inst, data);
    }

    /**
     * 型号联动销售价结算价，需要特殊处理
     * @param accessProdInst
     * @param offerId
     * @return
     */
    private String specialHandle(KiteProdInst accessProdInst, String offerId) {
        StringBuffer specialCode = new StringBuffer();
        specialCode.append(offerId).append(SymbolConsts.UNDERLINE).append(MODEL_FIELD_NAME).append(SymbolConsts.UNDERLINE);
        specialCode.append(Optional.ofNullable(accessProdInst.getAttr(MODEL_FIELD_NAME)).map(KiteAttr::getValue).orElse(""));
        return specialCode.toString();
    }

    private void dealAttrValue(KiteAttr attr, KiteInst inst, AttrData data, List<AttrValue> attrValueList) {
        if (KiteListUtils.isEmpty(attrValueList)) {
            return;
        }
        switch (attr.getAttrValueTypeId()) {
            case KeyConsts.DROPDOWN:
                List<KiteAttrValue> kiteAttrValueList = KiteBeanUtils.copyPropsForList(attrValueList, KiteAttrValue.class);
                attr.setAttrValues(kiteAttrValueList);
                attr.setAttrId(kiteAttrValueList.get(0).getAttrId());
                attr.setValue(kiteAttrValueList.get(0).getAttrValue());
                attr.setValueDesc(kiteAttrValueList.get(0).getAttrValueName());
                break;
            case KeyConsts.INPUT:
                attr.setValue(attrValueList.get(0).getAttrValue());
                attr.setValueDesc(attrValueList.get(0).getAttrValue());
                break;
            default:
                break;
        }
    }

    /**
     * 延期，重提初始化不处理
     * @param data
     * @return
     */
    private boolean isHandle(AttrData data) {
        boolean result = true;
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode")) {
            result = false;
        }
        //延期业务的场景初始化过程不处理
        if (KiteStringUtils.equalsAny(data.getServiceOfferId(), LocalServiceOfferConsts.SERVICE_OFFER_DELAY)
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            result = false;
        }
        return result;
    }
}
